<?php

namespace Modules\Admin\Http\Controllers;


use App\Services\AttachsDownloadLogService;
use App\Services\AttachService;

class AttachController extends Controller
{
    protected $attachService;

    public function __construct(AttachService $attacheService)
    {
        $this->attachService = $attacheService;
    }

    /**
     * @SWG\Post(
     *     path="/admin/attach/add",
     *     summary="附件上传",
     *     tags={"Admin/attach"},
     *     description="",
     *     produces={"application/json"},
     *     security={{"Bearer":{}}},
     *     @SWG\Response(
     *         response=200,
     *         description="code=0：操作成功；其他操作失败，message中有错误信息"
     *     )
     * )
     */
    public function add()
    {
        try {
            $res = $this->attachService->uploadImg('images', true, ['.doc','.docx','.xls','.xlsx','.zip','.rar'], 20*1024);
            return $this->ajaxReturn(0, '', $res);
        } catch (\Exception $e) {
            return $this->ajaxReturn(1001, $e->getMessage());
        }
    }

    /**
     * @SWG\Post(
     *     path="/admin/attach/remove/{uuid}",
     *     summary="删除附件",
     *     tags={"Admin/attach"},
     *     description="",
     *     produces={"application/json"},
     *     security={{"Bearer":{}}},
     *     @SWG\Response(
     *         response=200,
     *         description="code=0：操作成功；其他操作失败，message中有错误信息"
     *     )
     * )
     */
    public function remove($uuid)
    {
        try {
            if ($this->attachService->remove($uuid)) {
                return $this->ajaxReturn();
            }

            return $this->ajaxReturn(1001);
        } catch (\Exception $e) {
            return $this->ajaxReturn(1001, $e->getMessage());
        }
    }

    /**
     * @SWG\Post(
     *     path="/admin/attach/download/{id}",
     *     summary="附件下载",
     *     tags={"Admin/attach"},
     *     description="",
     *     produces={"application/json"},
     *     security={{"Bearer":{}}},
     *     @SWG\Response(
     *         response=200,
     *         description="code=0：操作成功；其他操作失败，message中有错误信息"
     *     )
     * )
     */
    public function download($id)
    {
        try {
            if ($res = $this->attachService->download($id)) {
                return $this->ajaxReturn(0, '', $res);
            }

            return $this->ajaxReturn(1001);
        } catch (\Exception $e) {
            return $this->ajaxReturn(1001, $e->getMessage());
        }
    }

    /**
     * @SWG\Get(
     *     path="/admin/attach/downFile/{uuid}",
     *     summary="附件下载",
     *     tags={"Admin/attach"},
     *     description="",
     *     produces={"application/json"},
     *     @SWG\Response(
     *         response=200,
     *         description="code=0：操作成功；其他操作失败，message中有错误信息"
     *     )
     * )
     */
    public function downFile($uuid)
    {
        try {
            $file = $this->attachService->downFile($uuid);
            return response()->download(storage_path('app/public/'.$file->file_path), $file->origin_name);
        } catch (\Exception $e) {
            return $this->ajaxReturn(1001, $e->getMessage());
        }
    }

    /**
     * @SWG\Post(
     *     path="/admin/attach/getList",
     *     summary="附件列表",
     *     tags={"Admin/attach"},
     *     description="",
     *     produces={"application/json"},
     *     security={{"Bearer":{}}},
     *     @SWG\Response(
     *         response=200,
     *         description="code=0：操作成功；其他操作失败，message中有错误信息"
     *     )
     * )
     */
    public function getList()
    {
        try {
            $per_page = $this->input('per_page', 20);
            $res = $this->attachService->getList($this->input('search'), $per_page);

            return $this->ajaxReturn(0, '', $res);
        } catch (\Exception $e) {
            return $this->ajaxReturn(1001, $e->getMessage());
        }
    }
}
